package cn.itsource.pethome.quartz.service.impl;

import cn.itsource.pethome.Basic.service.Impl.BaseServiceImpl;
import cn.itsource.pethome.quartz.domain.QuartzJobInfo;
import cn.itsource.pethome.quartz.job.QuartzJob;
import cn.itsource.pethome.quartz.service.IQuartzJobInfoService;
import cn.itsource.pethome.quartz.util.QuartzUtils;
import org.quartz.Scheduler;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.quartz.SchedulerFactoryBean;
import org.springframework.stereotype.Service;

@Service
public class QuartzJobInfoServiceImpl extends BaseServiceImpl<QuartzJobInfo> implements IQuartzJobInfoService {
    @Autowired
    private SchedulerFactoryBean schedulerFactoryBean;
    @Override
    public void addJob(QuartzJobInfo quartzJobInfo) {
        //获取scheduler调度器
        Scheduler scheduler = schedulerFactoryBean.getScheduler();
        //创建添加任务
        QuartzUtils.addJob(scheduler,quartzJobInfo.getJobName(), QuartzJob.class,quartzJobInfo,quartzJobInfo.getCronj());
    }

    @Override
    public void remove(String jobName) {
        //获取scheduler调度器
        Scheduler scheduler = schedulerFactoryBean.getScheduler();
        //删除定时器
        QuartzUtils.removeJob(scheduler,jobName);
    }
}
